mui.init({
	beforeback: function() {
		var parent = plus.webview.currentWebview().opener();
		//触发列表界面的自定义事件（reloadMethod）,从而进行数据刷新  
		mui.fire(parent, 'reloadView');
		//返回true，继续页面关闭逻辑
		return true;
	}
});
var imgNumber = 0;
var divSelectImg = mui("#div_selectImg")[0]; //添加图片的框框
var WORDS_NUM = 160;
var ID;
var item;

mui.plusReady(function() {
	var self = plus.webview.currentWebview();
	ID = self.ID;
	if(self.state){
		$("#div_bottomCancel").hide();
		$("#div_bottomSave").hide();
	}
	item = JSON.parse(self.item);
	initData();
	mui.previewImage();
	initListener();

});

function initData() {
	$("#eventtitile").find("a span").html(item.SJTITLE); //事件标题
	switch(item.SJLX) {
		case '10001': //事件
			$("#eventtype").find("a span").html("默认事件"); //事件类型
			break;
		case '10002': //告警
			$("#eventtype").find("a span").html("告警事件"); //事件类型
			break;
	}
	$("#eventtime").find("a span").html(item.LOGINDATE);
	$("#textarea_text").html(item.SJMS); //事件描述
	mui("#span_textNumber")[0].innerText = mui("#textarea_text")[0].value.length;
	$("#textarea_text_bz").html(item.BZ); //事件备注
	mui("#span_textNumber_bz")[0].innerText = mui("#textarea_text_bz")[0].value.length;
	$("#eventloginnname").find("a span").html(item.PERS_NAME); //登记人
	//加载问题附件
	var param = {
		guid: ID
	}
	ajax(GETBASE_IMAGE, param, function(data) {
		//图片附件
		if(data.successful = "true") {
			var photos = data.resultValue;
			if(photos && photos.length > 0) {
				for(var i = 0; i < photos.length; i++) {
					showImg("data:image/png;base64," + photos[i], "111");
				}
			}
		}
	});
	//加载历史反馈意见
	ajaxF1(GETFKD, param, function(result) {
		if(result.successful ==true) {
			var $myOrder11 = $("#dalao").find('li').eq(0);
			var data = result.resultValue;
			for(var i in data) {
				var $myOrder = $myOrder11.clone();
				$myOrder.find(".textarea_text_clyj").html(data[i].FKNR);
				$myOrder.find(".span_textNumber_clyj").eq(0).html(data[i].FKR);
				$("#ul_mysheet").append($myOrder);
			}
		}
	});
	//完成反馈和完成
	

}

function initListener() {
	// 加载图片预览功能
	// 设置底部栏位置，防止软键盘弹出时被顶上来
	if(window.innerWidth > 500) {
		mui("#div_bottomCancel")[0].style.top = (window.innerHeight - 0.96 * window.innerWidth / (7.2 * 1.5)) + "px";
		mui("#div_bottomSave")[0].style.top = (window.innerHeight - 0.96 * window.innerWidth / (7.2 * 1.5)) + "px";
	} else {
		mui("#div_bottomSave")[0].style.top = (window.innerHeight - 0.96 * window.innerWidth / 7.2) + "px";
		mui("#div_bottomCancel")[0].style.top = (window.innerHeight - 0.96 * window.innerWidth / 7.2) + "px";
	}
	//语音备注
	//	mui("#div_speechMask")[0].addEventListener("tap", function(e) {
	//		closeBlur(function() {
	//			if(mui("#textarea_text")[0].value.length >= WORDS_NUM) {
	//				mui.toast("已经达到字数上限了")
	//			} else {
	//				plus.speech.startRecognize(speechOptiion, function(text) {
	//					var wordText = mui("#textarea_text")[0].value + text
	//					if(wordText.length >= WORDS_NUM) {
	//						mui("#textarea_text")[0].value = wordText.substring(0, WORDS_NUM);
	//					} else {
	//						mui("#textarea_text")[0].value = wordText;
	//					}
	//					mui("#span_textNumber")[0].innerText = mui("#textarea_text")[0].value.length || 0;
	//				}, function(error) {
	//					console.log(JSON.stringify(error))
	//					mui.toast("语音识别失败：" + error.message);
	//				});
	//			}
	//		})
	//	});
	//手写备注
	mui("#textarea_text_clyj")[0].addEventListener("input", function() {
		if(mui.os.ios) {
			var count = this.value.split('\n').length - 1; //获取回车的个数
			mui("#span_textNumber_clyj")[0].innerText = this.value.length + count || 0;
		} else {
			mui("#span_textNumber_clyj")[0].innerText = this.value.length;
		}
	});

	//添加图片
	//	mui("#div_selectImg")[0].addEventListener("tap", function() {
	//		PickImage(true, function(srcArray) { //该方法会返回装有所选图片的地址的数组，第一个参数是是否多图片选择，第二个是成功回调
	//			mui.each(srcArray, function(i, item) {
	//				showImg(item); //展示所选图片
	//			})
	//		})
	//	});
	//删除照片
	//	mui("#div_imgLine").on("tap", ".div_delete_img", function() {
	//		var _this = this;
	//		imgNumber--;
	//		mui("#div_imgLine")[0].removeChild(_this.parentNode);
	//		if(imgNumber < 9) {
	//			mui("#div_imgLine")[0].appendChild(divSelectImg);
	//		}
	//	})
	$("#div_bottomCancel").on("tap", function() {
		var fkyj = $("#textarea_text_clyj").val();
		if(fkyj == "") {
			mui.toast("请填写处理意见");
			return;
		}
		var param = {
			fkyj: fkyj,
			guid: ID,
			persId: getUserModel().persId,
			state: "fk"
		}
		
		
		ajaxF1(POSTFANKUI, param, function(data) {
			//图片附件
			if(data.successful = "true") {
				mui.toast("反馈成功");
				mui.back();
			}
		});
	});
	

	$("#div_bottomSave").on("tap", function() {
		var fkyj = $("#textarea_text_clyj").val();
		if(fkyj == "") {
			mui.toast("请填写处理意见");
			return;
		}
		var param = {
			fkyj: fkyj,
			guid: ID,
			persId: getUserModel().persId,
			state: "wc"
		}
		ajaxF1(POSTFANKUI, param, function(data) {
			//图片附件
			if(data.successful ==true) {
				mui.toast("反馈成功");
				mui.back();
			}
		});
	})
}

//$(function() {
//	// 为每一个textarea绑定事件使其高度自适应
//	$.each($("textarea"), function(i, n) {
//		autoTextarea($(n)[0]);
//	});
//})


/**
 * 获取网络状态
 */
function getNetState() {
	var network = plus.networkinfo.getCurrentType();
	if(network == plus.networkinfo.CONNECTION_WIFI ||
		network == plus.networkinfo.CONNECTION_ETHERNET ||
		network == plus.networkinfo.CONNECTION_CELL2G ||
		network == plus.networkinfo.CONNECTION_CELL3G ||
		network == plus.networkinfo.CONNECTION_CELL4G) {
		return true;
	} else {
		return false;
	}
}

//网络状态改变监听
function onNetChange() {　　 // 获取当前网络类型
	var nt = plus.networkinfo.getCurrentType();　　
	switch(nt) {　　　　
		case plus.networkinfo.CONNECTION_ETHERNET:
		case plus.networkinfo.CONNECTION_WIFI:
		case plus.networkinfo.CONNECTION_CELL2G:
		case plus.networkinfo.CONNECTION_CELL3G:
		case plus.networkinfo.CONNECTION_CELL4G:
			//			mui(".net_unavailable")[0].classList.add("mui-hidden");　
			break;　　　　
		default:
			//			mui('.net_unavailable')[0].classList.remove("mui-hidden");　　　　　
			break;　　
	}
}

/**
 * 显示图片
 * @param {Object} url
 * @param {Object} imgID
 */

function showImg(url, imgID) {
	var nowTime = Date.parse(new Date()).toString(); // 时间戳
	var divImgLine = mui("#div_imgLine")[0];
	var divImgContainer = document.createElement('div');
	divImgContainer.className = "div_img_container";
	var divDeleteImg = document.createElement('div');
	divDeleteImg.className = "div_delete_img";
	if(imgID == undefined) { //详情显示不需要删除图片
		divDeleteImg.innerHTML = '<span class="icon-wrong"></span>';
	}
	var img = document.createElement('img');
	img.src = url;
	img.setAttribute("data-preview-src", "");
	img.setAttribute("data-preview-group", "1");
	if(imgID) {
		img.setAttribute("imgID", imgID);
	}
	img.onload = function() {
		console.log(url)
		if(!imgID) {
			plus.io.resolveLocalFileSystemURL(url, function(entry) {
				entry.file(function(file) {
					console.log(img.offsetHeight + "," + img.offsetWidth)
					if(img.offsetHeight > img.offsetWidth) {
						img.style.height = "auto";
						img.style.width = "100%";
					} else {
						img.style.height = "100%";
						img.style.width = "auto";
					}
					img.className = "img_style";
					img.fileName = file.name;
					var url2 = url.substr(0, url.lastIndexOf("/")) + "/" + nowTime + file.name; // 压缩成功后的图片地址
					plus.zip.compressImage({
						src: url,
						dst: url2,
						quality: 20,
						overwrite: true, // 压缩后的图片是否覆盖压缩前的图片
					}, function(event) {
						img.onload = function() {};
						img.src = url2;
						img.path = url2;
					}, function(error) {
						img.onload = function() {};
						img.src = url;
						img.path = url;
						console.log("图片压缩失败，将上传原图片");
					});
				});
			}, function(e) {
				console.log(e.message);
			});
		} else {
			console.log(img.height + "," + img.width)
			if(img.height > img.width) {
				img.style.height = "auto";
				img.style.width = "100%";
			} else {
				img.style.height = "100%";
				img.style.width = "auto";
			}
			img.className = "img_style";
		}
		if(divImgLine.children.length > imgNumber) {
			divImgLine.removeChild(divSelectImg);
		}
		divImgContainer.appendChild(img);
		divImgContainer.appendChild(divDeleteImg);
		divImgLine.appendChild(divImgContainer);
		imgNumber++;
		//		if(imgNumber < 9) {
		//			divImgLine.appendChild(divSelectImg);
		//		}
		console.log(divImgLine.children.length + "," + imgNumber);
	}
};